﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using WpfApp4.Models;
using WpfApp4.Models.Config;
using WpfApp4.Utilities;

using XEvent;
using XEvent.Events;

namespace WpfApp4.Views
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        private WindowEvent _windowEvent;

        public SettingWindow()
        {
            InitializeComponent();
            OnSubscribe();
        }

        private void OnSubscribe()
        {
            if (_windowEvent == null)
            {
                _windowEvent = XEventAgent.Instance.EventManager.GetEvent<WindowEvent>();
            }
            _windowEvent.Subscribe(OnCloseEvent, (p) => p.Type == EventType.Action && p.WindowActionType == WindowActionType.Close && p.EventId == EventId.SettingWindow);
        }

        private void OnCloseEvent(WindowEvent args)
        {
            var info = ConfigManager.LoadConfig();
            MenuInfo.Instance.MenuItems = info.MenuItems;
            MenuInfo.Instance.Settings = info.Settings;

            _windowEvent.Publish(new WindowEvent
            {
                Type = EventType.Action,
                WindowActionType = WindowActionType.Update,
                EventId = EventId.RefreshMenu,
            });

            Close();
        }
    }
}
